package com.lbd.system.controller;

import com.lbd.system.bean.Agreement;
import com.lbd.system.service.AgreementService;
import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.util.PdfUtils;
import org.apache.catalina.Server;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import static org.apache.tools.ant.types.resources.MultiRootFileSet.SetType.file;


@RestController
@RequestMapping("/agreement")
public class AgreementController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private AgreementService agreementService;

    /**
     * 按条件分页查询agreement
     *
     * @param page      第几页
     * @param rows      一页多少条
     * @param agreement
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Agreement agreement) {
        return new Result(ResultState.SUCCESS, "查询成功", agreementService.findByPageConditionJpql(page, rows, agreement));
    }

    /**
     * 保存agreement信息
     *
     * @param agreement
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Agreement agreement) {
        if (StringUtils.equals(agreement.getState(), "01")) {
            //发布
            agreement.setPublishTime(new Date());
        }
        agreementService.save(agreement);
        return new Result(ResultState.SUCCESS, "添加成功");
    }

    /**
     * 根据id查询回显要编辑的Agreement
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", agreementService.findById(id));
    }

    /**
     * 修改agreement信息
     *
     * @param agreement
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Agreement agreement) {
        agreementService.update(agreement);
        return new Result(ResultState.SUCCESS, "更新成功");
    }


}
